home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3049 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  73 lines

  1. Path: news.ems.psu.edu!usenet
  2. From: mahesh@cerse.psu.edu (Mahesh Venkitachalam)
  3. Newsgroups: comp.lang.c++
  4. Subject: Need help with overloading "<<"
  5. Date: 21 Jan 1996 22:32:00 GMT
  6. Organization: PSU - Earth and Mineral Sciences
  7. Message-ID: <4duet0$ju9@wegener.ems.psu.edu>
  8. Reply-To: mahesh@cerse.psu.edu
  9. NNTP-Posting-Host: eagle.cerse.psu.edu
  10.  
  11. Hi !
  12.  
  13. I was trying to overload the "<<" operator for a Complex class, 
  14. without much sucess. Could someone please help me out ?
  15.  
  16. In the main program, when I do 
  17.  
  18. cout << a << endl ; ( here a is defined as   Complex a(1.0, 1.0) ;)
  19.  
  20. the compiler gives the following error :
  21.  
  22. "prog2.C", line 11.21: 1540-070: (S) Call does not match any argument list 
  23.  
  24. for "ostream::operator<<".
  25.  
  26.  
  27. The overloading function is :
  28.  
  29. ostream& operator<<(ostream& s, Complex& z)
  30. {
  31.  s << z.real() << " + " << z.img() <<"i" << endl;
  32.  return s;
  33.  
  34.  
  35. and the class definition is :
  36.  
  37. // complex1.h
  38.  
  39. class Complex
  40. {
  41.   private:
  42.       
  43.       double real_;
  44.       double img_;
  45.       
  46.   public:
  47.   
  48.       Complex();                // Constuctors
  49.       Complex(double real, double img);
  50.       
  51.       Complex operator+ (Complex& z);    // operations
  52.       
  53.       double real() {return real_;}             
  54.       double img() {return img_ ;}
  55.       
  56.       void cprint();
  57.       
  58.       ~Complex() ;            // destructor
  59.       
  60. }  ;
  61.  
  62.  
  63. I have been stuck with this for some time now...
  64.  
  65.  
  66. Thanks a lot
  67.  
  68. Mahesh
  69.  
  70.  
  71.  
  72.